package ee.videorentalstore.utils;

import java.math.BigDecimal;

import ee.videorentalstore.inventories.FilmType;

public class PriceCalculator 
{
	private static final BigDecimal REGULAR_FEE = new BigDecimal("30");
	private static final BigDecimal PREMIUM_FEE = new BigDecimal("40");
	
	private static final int NUMBER_OF_FIRST_DAYS_WHEN_REQUISITION_IS_CONSTANT_FOR_REGULAR_FILMS = 3;
	private static final int NUMBER_OF_FIRST_DAYS_WHEN_REQUISITION_IS_CONSTANT_FOR_OLD_FILMS = 5;
	
	
	public static BigDecimal calculateRentalFee(int numberOfRentalDays, FilmType filmType)
	{
		switch (filmType) 
		{
			case NEW_RELEASE:
				return calculateRentalFeeForNewReleases(numberOfRentalDays);
			case REGULAR_FILM:
				return calculateRentalFeeWhenRequisitionIsConstantForGivenNumberOfFirstDays(numberOfRentalDays,
						NUMBER_OF_FIRST_DAYS_WHEN_REQUISITION_IS_CONSTANT_FOR_REGULAR_FILMS);
			case OLD_FILM:
				return calculateRentalFeeWhenRequisitionIsConstantForGivenNumberOfFirstDays(numberOfRentalDays, 
						NUMBER_OF_FIRST_DAYS_WHEN_REQUISITION_IS_CONSTANT_FOR_OLD_FILMS);
			default:
				System.out.println("ERROR: Film type has to be valid when calculating rental fee!");
				return null;
		}
	}
	
	
	private static BigDecimal calculateRentalFeeForNewReleases(int numberOfRentalDays)
	{
		return PREMIUM_FEE.multiply(new BigDecimal(numberOfRentalDays));
	}
	
	
	private static BigDecimal calculateRentalFeeWhenRequisitionIsConstantForGivenNumberOfFirstDays(
			int numberOfRentalDays, int givenNumberOfFirstDaysWhenRequisitionIsConstant)
	{
		int numberOfRentalDaysExceedingGivenNumberOfFirstDaysWhenRequisitionIsConstant = 
				numberOfRentalDays - givenNumberOfFirstDaysWhenRequisitionIsConstant;
		if (numberOfRentalDaysExceedingGivenNumberOfFirstDaysWhenRequisitionIsConstant > 0)
		{
			return REGULAR_FEE.add(REGULAR_FEE.multiply(
					new BigDecimal(numberOfRentalDaysExceedingGivenNumberOfFirstDaysWhenRequisitionIsConstant)));
		}
		else
		{
			return REGULAR_FEE;
		}
	}
}
